package go_leetcode

import "golang.org/x/exp/slices"

func closeStrings(word1 string, word2 string) bool {
	if len(word1) != len(word2) {
		return false
	}

	var oneMark, twoMark int
	var oneCnt, twoCnt [26]int
	for _, c := range word1 {
		oneMark |= 1 << (c - 'a')
		oneCnt[c-'a']++
	}

	for _, c := range word2 {
		twoMark |= 1 << (c - 'a')
		twoCnt[c-'a']++
	}

	slices.Sort(oneCnt[:])
	slices.Sort(twoCnt[:])
	return oneMark == twoMark && slices.Equal(oneCnt[:], twoCnt[:])
}